﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoogleSearchAPI.Query;
using GoogleSearchAPI.Resources;
using GoogleSearchAPI;
using System.Reflection;
using System.Diagnostics;
using System.Net;

namespace Google.Search.Dotnet
{
    class Program
    {
        static void Main(string[] args)
        {
            WebQuery query = new WebQuery("senac");
            
            query.StartIndex.Value = 0;
            query.ResultSetSize.Value = ResultSetSize.large;
            query.HostLangauge.Value = Languages.English;
            QueryCountryCodeParameter q = new QueryCountryCodeParameter();
            q.Value = "br";
            query.CountryCode = q;
            //query.APIKey = new QueryAPIKeyParameter(
            QueryAPIKeyParameter qp = new QueryAPIKeyParameter();


            System.IO.TextWriter arquivo = null;
            try
            {
                //Console.WriteLine("Informe o caminho completo com o nome do arquivo e extensão:");
                string nome_arquivo = "C:/arquivo.txt";
                //string nome_arquivo = Console.ReadLine();

                if (!System.IO.File.Exists(nome_arquivo))
                    System.IO.File.Create(nome_arquivo).Close();
                
                arquivo = System.IO.File.AppendText(nome_arquivo);

                int cont = 0;

                int[] offsets = { 0, 8, 16, 24, 32, 40, 48 };
                foreach (var offset in offsets)
                {
                    query.StartIndex.Value = offset;
                    IGoogleResultSet<GoogleWebResult> resultSet = GoogleService.Instance.Search<GoogleWebResult>(query);

                    foreach (GoogleWebResult item in resultSet.Results)
                    {
                        cont++;
                        Console.WriteLine(cont + item.Url.ToString());
                        arquivo.WriteLine(cont + "Url: " + item.Url.ToString());
                    }
                }
            }
            catch (Exception exc)
            {
                throw new Exception("Atenção! Erro! ==> " + exc.Message);
            }
            finally
            {
                arquivo.Close();
            }

            Console.ReadKey();
        }
    }
}
